//
//  TTRecyclingShareView.m
//  TaoTuo
//
//  Created by apple on 2020/9/15.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTRecyclingShareView.h"
#import "TTShopCartView.h"
#import <WXApi.h>

#define animationDuration 0.3

@interface TTRecyclingShareView ()

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIView *bottomWhiteView;


@end

@implementation TTRecyclingShareView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setupSubviews];
        
        self.hidden = YES;
        
    }
    return self;
}

/// 取消
- (void)cancelAction
{
    [self dismiss];
}

- (void)setImage:(UIImage *)image
{
    _image = image;
    self.imageView.image = self.image;
    
    CGSize imageSize = image.size;
    self.imageView.width = imageSize.width * self.imageView.height / imageSize.height;
    CGFloat x = (self.width - self.imageView.width) * 0.5;
    self.imageView.left = x;
    
    [self addAnimations];
    
}

/// 分享图片到微信
- (void)shareImageWithType:(int)type
{
    if (![WXApi isWXAppInstalled]) {
        [[UIApplication sharedApplication].keyWindow makeToast:@"未安装微信"];
        return;
    }
    
    NSData *imageData = UIImageJPEGRepresentation(self.image, 0.7);
    WXImageObject *imageObject = [WXImageObject object];
    imageObject.imageData = imageData;

    WXMediaMessage *message = [WXMediaMessage message];
//    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"res5"
//                                                         ofType:@"jpg"];
//    message.thumbData = [NSData dataWithContentsOfFile:filePath];
    message.mediaObject = imageObject;

    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = message;
    req.scene = type;
    [WXApi sendReq:req completion:^(BOOL success) {
        [[UIApplication sharedApplication].keyWindow makeToast:@"分享成功"];
        [self dismiss];
    }];
    
}

- (void)setupSubviews
{
    _bottomWhiteView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height - 233*kScale - Tabbar_Bottom_height, self.width, 233*kScale + Tabbar_Bottom_height)];
    _bottomWhiteView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_bottomWhiteView];
    
    UIButton *cancelBtn = [UIButton createButtonWithTitle:@"取消" titleColor:UIColorHex(#999999) titleFontSize:15 borderColor:UIColorHex(#979797) cornerRadius:22.5*kScale contentEdgeInsets:UIEdgeInsetsZero target:self action:@selector(cancelAction)];
    cancelBtn.frame = CGRectMake(43*kScale, _bottomWhiteView.height - Tabbar_Bottom_height - 10 - 45*kScale, SCREEN_WIDTH - 86*kScale, 45*kScale);
    [_bottomWhiteView addSubview:cancelBtn];
    
    @weakify(self)
    TTShopCartView *friendsView = [[TTShopCartView alloc] initWithFrame:CGRectMake(0, cancelBtn.top - 12 - 70*kScale, self.width * 0.5, 70*kScale) imageName:@"weixin" title:@"微信好友" clickShopCartBlock:^(TTShopCartView *cartView) {
        @strongify(self)
        [self shareImageWithType:WXSceneSession];
        
    }];
    [_bottomWhiteView addSubview:friendsView];
    
    TTShopCartView *timeLineView = [[TTShopCartView alloc] initWithFrame:CGRectMake(friendsView.right, friendsView.top, self.width * 0.5, friendsView.height) imageName:@"pengyouquan" title:@"朋友圈" clickShopCartBlock:^(TTShopCartView *cartView) {
        @strongify(self)
        [self shareImageWithType:WXSceneTimeline];
    }];
    [_bottomWhiteView addSubview:timeLineView];
    
    CGFloat w = 300*kScale;
    CGFloat h = 460*kScale;
    CGFloat x = (self.width - w) * 0.5;
    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, 0, w, h)];
    _imageView.bottom = _bottomWhiteView.top + 60 * kScale - Tabbar_Bottom_height;
//    [_imageView setLayerShadow:UIColorHex(#E7E7E7) offset:CGSizeZero radius:10];
    _imageView.layer.cornerRadius = 10;
    _imageView.layer.masksToBounds = YES;
    _imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:_imageView];
    
    
}

- (void)addAnimations
{
    self.hidden = NO;
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
    self.bottomWhiteView.top = self.bottom;
    self.imageView.transform = CGAffineTransformMakeScale(0.3, 0.3);
    self.imageView.alpha = 0.3;
    [UIView animateWithDuration:animationDuration animations:^{
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        self.bottomWhiteView.bottom = self.bottom;
        self.imageView.transform = CGAffineTransformMakeScale(1, 1);
        self.imageView.alpha = 1;
    }];
    
}

- (void)dismiss
{
    [UIView animateWithDuration:animationDuration animations:^{
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
        self.bottomWhiteView.top = self.bottom;
        self.imageView.transform = CGAffineTransformMakeScale(0.3, 0.3);
        self.imageView.alpha = 0.3;
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    if (CGRectContainsPoint(self.bottomWhiteView.frame, point) || CGRectContainsPoint(self.imageView.frame, point)) {
        return;
    }
    
    [self dismiss];
    
}


@end
